package liu.xian.qingliu;


import liu.xian.Const;
import liu.xian.retrofitgson.GsonConverterFactory;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

import java.net.InetSocketAddress;
import java.net.Proxy;

/**
 * [description]
 *
 * @Author liuxian
 * @Date 2021/2/8 8:23 下午
 */

public class Qingliu {
    public static OkHttpClient client;

    static {
        Proxy proxy = Proxy.NO_PROXY;
        if (System.getProperty("http.proxyHost") != null && System.getProperty("http.proxyPort") != null) {
            try {
                proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(System.getProperty("http.proxyHost"),
                        Integer.valueOf(System.getProperty("http.proxyPort"))));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        client = new OkHttpClient.Builder()
                .proxy(proxy)
                .addInterceptor(new QingliuRequestInterceptor())
                .build();
    }

   public static <T> T getApi(Class<T> apiClass) {
        Retrofit retrofit = new Retrofit
                .Builder()
                .baseUrl(Const.HOST)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return retrofit.create(apiClass);
    }
}
